/**
 * 工具箱
 * Created by jaosnZhang on 2018/4/8.
 */

let tools = {
  /**
   * @description  生成随机字符串
   * @param {String} n 随机字符串长度
   */
  generateMixed: function(n) {
    var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    var res = "";
    for(var i = 0; i < n; i++) {
      var id = Math.ceil(Math.random() * 35);
      res += chars[id];
    }
    return res;
  },

  /**
   * @description 清除参数对象空参数
   * @param {Object} currentParam 对象参数 
   */
  removeEmpyParam: function(currentParam) {
    var Parameter = {};
    for(i in currentParam) {
      if(currentParam[i] && currentParam[i] != "undefined" && currentParam[i] != "null" && currentParam[i] != "{}" && currentParam[i] != "") {
        Parameter[i] = currentParam[i];
      }
    }
    return Parameter;
  },

  /**
   * @description  根据ASCII排序参数对象
   * @param {Object} dict 键值对字典参数对象
   */
  sortByASCII: function(dict) {
    var stringAsc = '',
      keys = Object.keys(dict).sort();
    for(var i = 0, n = keys.length, key; i < n; ++i) {
      stringAsc = stringAsc + keys[i] + '=' + dict[keys[i]] + '&'
    }
    stringAsc = stringAsc.substring(0, stringAsc.length - 1);
    return stringAsc;
  }
};

module.exports = tools;